import org.mozilla.interfaces.nsIDOMCSS2Properties;
import org.mozilla.interfaces.nsIDOMCSSStyleDeclaration;
import org.mozilla.interfaces.nsIDOMDocument;
import org.mozilla.interfaces.nsIDOMDocumentTraversal;
import org.mozilla.interfaces.nsIDOMDocumentView;
import org.mozilla.interfaces.nsIDOMElement;
import org.mozilla.interfaces.nsIDOMNode;
import org.mozilla.interfaces.nsIDOMNodeFilter;
import org.mozilla.interfaces.nsIDOMTreeWalker;
import org.mozilla.interfaces.nsIDOMViewCSS;

public class DOMWalker {
	private final nsIDOMDocument document;
	private final nsIDOMTreeWalker aWalker;
	private final DrawingAnalyzer drawingAnalyzer;
	private final String drawIdAttrName;
	private final String layerIdAttrName;
	public DOMWalker(final nsIDOMDocument document, boolean createDrawId, boolean createLayerId
			, String drawIdAttrName, String layerIdAttrName) {
		this.document = document;
		this.drawIdAttrName = drawIdAttrName;
		this.layerIdAttrName = layerIdAttrName;
		final nsIDOMDocumentTraversal traversal = (nsIDOMDocumentTraversal)
				document.queryInterface(nsIDOMDocumentTraversal.NS_IDOMDOCUMENTTRAVERSAL_IID);
		aWalker = traversal.createTreeWalker(document, nsIDOMNodeFilter.SHOW_ELEMENT, null, true);
		this.drawingAnalyzer = new DrawingAnalyzer(createDrawId, createLayerId);
	}
	nsIDOMViewCSS viewCss = null;
	public void walk() {
		final nsIDOMDocumentView documentView = (nsIDOMDocumentView)document
				.queryInterface( nsIDOMDocumentView.NS_IDOMDOCUMENTVIEW_IID );
		viewCss = (nsIDOMViewCSS)documentView.getDefaultView()
			.queryInterface( nsIDOMViewCSS.NS_IDOMVIEWCSS_IID );
		
		_walk();
		drawingAnalyzer.compute();
	}
	private void _walk() {
final nsIDOMNode currNode = aWalker.getCurrentNode();
Object DrawingElementId = null;
switch (currNode.getNodeType()) {
case nsIDOMNode.ELEMENT_NODE: {
	final nsIDOMElement currElement = (nsIDOMElement)currNode.queryInterface(nsIDOMElement.NS_IDOMELEMENT_IID);
	final nsIDOMCSSStyleDeclaration compStyle = viewCss.getComputedStyle(currElement, null);
	final nsIDOMCSS2Properties css2Prop = (nsIDOMCSS2Properties)compStyle.queryInterface(nsIDOMCSS2Properties.NS_IDOMCSS2PROPERTIES_IID);
	DrawingElementId = drawingAnalyzer.goDown(currElement.getNodeName(), css2Prop.getPosition()
	, css2Prop.getZIndex(), css2Prop.getCssFloat(), css2Prop.getDisplay()
	, new IProcedure<Integer>() {
		public void apply(Integer avar) {
			currElement.setAttribute(drawIdAttrName, avar.toString());
		}
		}
	, new IProcedure<Integer>() {
		public void apply(Integer avar) {
			currElement.setAttribute(layerIdAttrName, avar.toString());
		}
		}
	);
}
}
for (nsIDOMNode childNode = aWalker.firstChild(); childNode!=null; childNode = aWalker.nextSibling()) {
	_walk();
}
drawingAnalyzer.goUp(DrawingElementId);
aWalker.setCurrentNode(currNode);
	}
}
